﻿using System;
using System.Windows;
using System.Windows.Interactivity;
using System.Windows.Media.Animation;
using PairingMatrix.ViewModel;

namespace PairingMatrix.View.Behavior
{
    public class ShowProjectCardNotesBehavior : Behavior<ProjectCardControl>
    {
        private ProjectCardViewModel _projectCardViewModel;
        private Storyboard _storyboardHeight;
        private DoubleAnimation _heightAnimation;

        protected override void OnAttached()
        {
            AssociatedObject.Loaded += (sender, e) =>
            {
                _projectCardViewModel = (ProjectCardViewModel)AssociatedObject.DataContext;
                _projectCardViewModel.PropertyChanged += ProjectCardPropertyChanged;

                //animation for pairwith controls
                _heightAnimation = new DoubleAnimation();
                TimeSpan duration = TimeSpan.FromMilliseconds(100);
                _heightAnimation.Duration = duration;
                _storyboardHeight = new Storyboard();
                _storyboardHeight.Duration = duration;
                _storyboardHeight.Children.Add(_heightAnimation);
                Storyboard.SetTarget(_heightAnimation, AssociatedObject.notes);
                Storyboard.SetTargetProperty(_heightAnimation, new PropertyPath("Height"));

                ShowHideNotes();
            };

        }

        void ProjectCardPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "ShowNotes")
            {
                ShowHideNotes();
                
            }
        }

        private void ShowHideNotes()
        {
            if (_projectCardViewModel.ShowNotes)
            {
                _heightAnimation.To = 50.0;
                _storyboardHeight.Begin();
            }
            else
            {
                _heightAnimation.To = 0.0;
                _storyboardHeight.Begin();
            }
        }
    }
}
